﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;

namespace MyChart
{
    public class FxpChart : ChartBase
    {
        public List<FxpDzBoard> DzBoardList = new List<FxpDzBoard>();
        DataTable m_dt;
        int m_namex;
        int m_namey;
        int m_preDzRows;

        public FxpChart()
        {
            ReadCfg();
        }

        public FxpChart(DataTable dt)
        {
            m_dt = dt;
            ReadCfg();
            InitChart(dt);
        }

        private void ReadCfg()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("./\\fxp.xml");
            XmlNode root = doc.DocumentElement;
            XmlNode node2;//= root.SelectSingleNode("descendant::分线盘/端子/@距离名称x");
            node2 = root.SelectSingleNode("descendant::分线盘/端子/@距离名称x");
            int.TryParse(node2.Value.ToString(), out m_namex);
            node2 = root.SelectSingleNode("descendant::分线盘/端子/@距离名称y");
            int.TryParse(node2.Value.ToString(), out m_namey);
            node2 = root.SelectSingleNode("descendant::分线盘/端子/@每个端子所占行数");
            int.TryParse(node2.Value.ToString(), out m_preDzRows);
        }

        public FxpChart(string fxpHeadName, int cengCount, int preCengDzBoard, DataTable dt)
        {
            m_dt = dt;
            ReadCfg();
        }

        public override void InitChart(DataTable dt)
        {
            int fxpStartRow = 0;
            string fxpName = "";
            int rowIndex;
            int id = 0;
            for (rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
            {

                DataRow row = dt.Rows[rowIndex];
                if (rowIndex >= fxpStartRow)
                {
                    #region 查找分线盘层的名称
                    foreach (DataColumn col in dt.Columns)
                    {
                        if (string.IsNullOrEmpty(row[col].ToString())) continue;
                        if (row[col].ToString().Contains("F"))
                        {
                            fxpName = row[col].ToString();
                            id = 0;
                            fxpStartRow += 16;
                        }
                    }
                    if (fxpName != "")
                    {
                        string[] fxpNames = fxpName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string str in fxpNames)
                        {
                            if(str.Contains("F"))
                            {
                               fxpName = str;
                               break;
                            }
                        }
                    }
                    #endregion
                }

                string strid;
                foreach (DataColumn col in dt.Columns)
                {
                    string strVaule = row[col].ToString().Trim();
                    strVaule = strVaule.Replace(" ", "");
                    if (strVaule == "名称")
                    {
                        strid = ++id > 9 ? fxpName + id.ToString() : fxpName + "0" + id.ToString();
                        FxpDzBoard fxpDzBoard = new FxpDzBoard(strid, m_namex, m_namey, m_preDzRows, rowIndex, dt.Columns.IndexOf(col));
                        DzBoardList.Add(fxpDzBoard);
                        fxpDzBoard.InitChart(dt);
                        fxpStartRow = Math.Max(fxpStartRow, fxpDzBoard.FxpDzs[5].NameRow + 2);
                    }
                }

            }
        }
    }
}
